package com.atguigu.day26.problem03;
/*
批处理:
    向account 表 插入10000条记录

 */

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class BatchTest {

    @Test
    public void test02() throws Exception{
        long start = System.currentTimeMillis();
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu?rewriteBatchedStatements=true", "root", "1234");
        //3.准备sql
        String sql = "insert into t_account values(null,?,?)";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.填充数据
        for (int i = 1; i <=10000 ; i++) {
            pst.setObject(1,"艾跃进"+i);
            pst.setObject(2,i);
            //6.添加到批处理
            pst.addBatch();
        }
        //执行批处理
        pst.executeBatch();
        //7.关闭资源
        pst.close();
        connection.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);//67902  68864  614


    }


    @Test
    public void test01() throws Exception{
        long start = System.currentTimeMillis();
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "root");
        //3.准备sql
        String sql = "insert into t_account values(null,?,?)";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.填充数据
        for (int i = 1; i <=10000 ; i++) {
            pst.setObject(1,"安琪拉"+i);
            pst.setObject(2,i);
            //6.执行命令
            pst.executeUpdate();
        }
        //7.关闭资源
        pst.close();
        connection.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);//67902

    }

}
